package com.lc.luzhengtang.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lc.luzhengtang.R;


/**
 * Created by junLong on 2017/4/10.
 */

public class ShowPopuBuyOilCardPopu extends PopupWindow implements View.OnClickListener{

    private View view;
    private int num = 1;
    private final TextView tvNum;
    private final ImageButton inDown;
    private int state;

    public ShowPopuBuyOilCardPopu(int state, Context context, View.OnClickListener listener) {
        this.state=state;
        this.view = LayoutInflater.from(context).inflate(R.layout.popu_buy_oil_car_layout, null);
        ImageView ivClose = (ImageView) this.view.findViewById(R.id.iv_close);
        ivClose.setOnClickListener(this);
        ImageButton ibUp = (ImageButton) this.view.findViewById(R.id.ib_up);
        inDown = (ImageButton) this.view.findViewById(R.id.ib_down);
        inDown.setOnClickListener(this);
        tvNum = (TextView) this.view.findViewById(R.id.tv_goods_num);
        ibUp.setOnClickListener(this);
        TextView ensure_tv= (TextView) view.findViewById(R.id.ensure_tv);
        switch (state) {
            case 0:
                ensure_tv.setText("确定");
                break;
            case 1:
                ensure_tv.setText("确定");
                break;
        }


        RelativeLayout rlEnsure = (RelativeLayout) this.view.findViewById(R.id.rl_ensure_oil);
        rlEnsure.setOnClickListener(listener);
        // 设置外部可点击
        this.setOutsideTouchable(true);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.view.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = view.findViewById(R.id.pop_layout).getTop();

                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return false;
            }
        });

        /* 设置弹出窗口特征 */
        // 设置视图
        this.setContentView(this.view);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);

        // 设置弹出窗体可点击
        this.setFocusable(true);

        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x80000000);
        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);

        // 设置弹出窗体显示时的动画，从底部向上弹出
        this.setAnimationStyle(R.style.take_photo_anim);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ib_up:
                this.num++;
                tvNum.setText(String.valueOf(this.num));
                break;
            case R.id.ib_down:
                if (num>=1){
                    this.num--;
                    tvNum.setText(String.valueOf(this.num));
                }
                break;
            case R.id.iv_close:
                this.dismiss();
                break;
        }
    }

    public String getNums(){
        return String.valueOf(num);
    }
    public int getState(){
        return state;
    }
}
